package com.collect.discord.web.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.collect.common.eventbus.annotation.EventBusListenerAnnotation;
import com.collect.common.eventbus.enums.EventBusMode;
import com.collect.common.eventbus.message.EventBusMessage;
import com.collect.common.redis.RedisCache;
import com.collect.common.redis.RedisKeyUtils;
import com.collect.common.utils.date.DateUtils;
import com.collect.common.utils.eventbus.EventBusUtils;
import com.collect.common.utils.http.HttpClientUtils;
import com.collect.discord.web.domain.BinanceNewestPrice;
import com.collect.discord.web.domain.BinancePrice;
import com.collect.discord.web.mapper.BinancePriceMapper;
import com.collect.discord.web.service.IBinancePriceService;
import com.google.common.eventbus.Subscribe;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * @Author :hky
 * @ Date  : Created in 10:36 2022-09-27
 * @ Version discord
 * @ Decribtion : $
 */
@Service
@EventBusListenerAnnotation(mode = EventBusMode.ASYNC)
public class BinancePriceServiceImpl extends ServiceImpl<BinancePriceMapper, BinancePrice> implements IBinancePriceService {
    @Resource
    private RedisCache redisCache;
    @Resource
    private BinancePriceMapper binancePriceMapper;

    @Value("${binance.newestPriceUrl}")
    private String url;

    /**
     *
     * @param sensitiveList 关键字数组
     * @param isEntence 直接调用接口
     * @return
     */
    @Override
    public List<BinanceNewestPrice> getNewestPriceList(List<String>  sensitiveList,boolean isEntence) {
        List<BinanceNewestPrice> reusltList=new ArrayList<>();
        if(isEntence){
            Map<String, String> param=new HashMap<>();
            if(CollectionUtils.isNotEmpty(sensitiveList)){
                param.put("symbols",sensitiveList.toString());
            }
            String result= HttpClientUtils.doGet(url, param,null,true);
            reusltList= JSONArray.parseArray(result, BinanceNewestPrice.class);
        }else{
            if(CollectionUtils.isNotEmpty(sensitiveList)){
                for (String symbol : sensitiveList) {
                    BinanceNewestPrice newestPrice = redisCache.getCacheMapValue(RedisKeyUtils.NEWESTPRICE, symbol);
                    reusltList.add(newestPrice);
                }
            }else{
                reusltList = redisCache.getAllMapValue(RedisKeyUtils.NEWESTPRICE);
            }
        }
        return reusltList;
    }

    /**
     * 异步保存币安价格信息
     * @param msg
     * @throws ParseException
     */
    @Subscribe
    public void syncSaveBinancePrice(EventBusMessage<BinanceNewestPrice> msg) throws ParseException {
        if (EventBusUtils.BINANCEMESSAGE.equals(msg.getCode())) {
            List<BinanceNewestPrice> bodyList = msg.getBodyList();
            if(CollectionUtils.isNotEmpty(bodyList)){
                List<BinancePrice> resultList=new ArrayList<>(bodyList.size());
                Date lDate = DateUtils.getLDate();
                Date nowDate = DateUtils.getNowDate();
                bodyList.forEach(e ->{
                    BinancePrice binancePrice=new BinancePrice();
                    binancePrice.setSymbol(e.getSymbol());
                    binancePrice.setPrice(e.getPrice());
                    binancePrice.setRecieveDate(lDate);
                    binancePrice.setCreateTime(nowDate);
                    resultList.add(binancePrice);
                });
                //保存币安的价格信息,不使用mybatisplus的，太慢
                binancePriceMapper.batchSaveBinancePrice(resultList);
            }
        }
    }

}
